package com.exam.controller;

import com.exam.dto.ActionResult;
import com.exam.po.Announcement;
import com.exam.po.Student;
import com.exam.po.Teacher;
import com.exam.service.TeacherService;
import com.exam.ustudent.UStudent;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author zhanghui
 * @ClassName TeacherController
 * @Description TODO
 * @Date 2021/1/11 20:04
 * @Version 1.0
 **/

@RestController
@RequestMapping("/api")
public class TeacherController {
    @Autowired
    private TeacherService teacherService;

    /**
     * @Author zhanghui
     * @Description 根据教师课程分页查询学生
     * @Date 2021/1/11 19:57
     * @Param [page, size]
     * @return com.exam.dto.ActionResult
     **/
    @GetMapping("/teacher/findStudents/{teacherId}/{page}/{size}")
    public ActionResult findAllByPage(@PathVariable Integer teacherId,@PathVariable Integer page, @PathVariable Integer size) {
        Map<String,Object> map = new HashMap<>();
        Integer total = teacherService.findCountStudents(teacherId);
        List<UStudent> res = teacherService.findStudentByCourse(teacherId, page, size);
        map.put("total",total);
        map.put("data",res);
        if(map!=null){
            return new ActionResult(200, "分页查询所有学生", map);
        }
        return new ActionResult(400, "分页查询失败", null);
    }
    /**
     * @Author zhanghui
     * @Description TODO
     * @Date 2021/1/12 9:42
     * @Param [page, size]
     * @return com.exam.dto.ActionResult
     **/
    @GetMapping("/teacher/checkAllAnnouncement")
    @ResponseBody
    public ActionResult findAllAnnouncement(){
        List<Announcement> res = teacherService.findAnnouncements();
        if (res!=null){
            return new ActionResult(200,"查询公告成功",res);
        }else {
            return new ActionResult(404,"查询公告失败",null);
        }
    }
    /*
     * @Author mahaoqi
     * @Description 添加教师
     * @Date 2021/1/11 19:03
     * @Param [teacher]
     * @return com.exam.dto.ActionResult
     **/
    @RequestMapping(value = "/addTeacher",method = RequestMethod.POST)
    public ActionResult addTeacher(@RequestBody Teacher teacher){
        if(teacherService.addTeacher(teacher)){
            return new ActionResult(200,"添加成功!",null);
        }
        return new ActionResult(500,"添加失败!",null);
    }

    /*
     * @Author mahaoqi
     * @Description 根据id删除教师
     * @Date 2021/1/11 19:10
     * @Param [teacherId]
     * @return com.exam.dto.ActionResult
     **/
    @RequestMapping(value = "/delTeacher/{teacherid}",method = RequestMethod.DELETE)
    public ActionResult delTeacher(@PathVariable Integer teacherid){
        if(teacherService.removeTeacher(teacherid)){
            return new ActionResult(200,"删除成功!",null);
        }
        return new ActionResult(500,"添加失败",null);
    }

    /*
     * @Author mahaoqi
     * @Description 修改
     * @Date 2021/1/11 19:19
     * @Param [teacher]
     * @return com.exam.dto.ActionResult
     **/
    @RequestMapping(value = "/modifyTeacher",method = RequestMethod.PUT)
    public ActionResult modifyTeacher(@RequestBody  Teacher teacher){
        if(teacherService.modifyTeacher(teacher)){
            return new ActionResult(200,"修改成功!",null);
        }
        return new ActionResult(500,"修改失败",null);
    }

    /*
     * @Author mahaoqi
     * @Description 分页查询
     * @Date 2021/1/11 19:32
     * @Param [page, pageSize]
     * @return com.exam.dto.ActionResult
     **/

    @RequestMapping(value = "/findTeacherList/{current}/{size}",method = RequestMethod.GET)
    @ResponseBody
    public ActionResult findTeacherList(@PathVariable Integer current,@PathVariable Integer size){
        Map map = new HashMap();
        List<Teacher> teacherList = teacherService.findList(current,size);
        int count = teacherService.findCount();
        map.put("data",teacherList);
        map.put("total",count);
        if(map!=null){
            return new ActionResult(200,"查询成功!",map);
        }
        return new ActionResult(404,"未找到资源!",null);
    }

    /*
     * @Author mahaoqi
     * @Description TODO
     * @Date 2021/1/11 19:48
     * @Param []
     * @return com.exam.dto.ActionResult
     **/
    @RequestMapping(value = "/findTeacherCount",method = RequestMethod.GET)
    public ActionResult findTeacherCount(){
        int count = teacherService.findCount();
        return new ActionResult(200,"查询成功!",count);
    }
}
